/*********************/
/** ProcessLightInfo.java */
/*********************/
package atag.apps.mainApp;

import atag.runtime.DataItem;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;

public class ProcessLightInfo implements Runnable {

	private final DataPool m_dataPool;
	private final NodeInfo m_myState;
	private int lightSetting = Constants.DEFAULT_SETTING;
	private int lightReading;

	/** Creates a new instance of ProcessLightInfo */
	public ProcessLightInfo(DataPool dp, NodeInfo myconfig) {
		m_dataPool = dp;
		m_myState = myconfig;
	}

	public void run() {
		/* Checking for each DataItem, to see which one was produced */
		DataItem t_dataItem;
		if ((t_dataItem = m_dataPool.getData(IDConstants.T_PROCESSLIGHTINFO,
				IDConstants.D_LIGHTREADING)) != null) {
			LightReading recvdLightReading = (LightReading) t_dataItem.core();
			this.handleLightReadingProduced(recvdLightReading);
		} else if ((t_dataItem = m_dataPool.getData(
				IDConstants.T_PROCESSLIGHTINFO, IDConstants.D_RFIDLIGHTSETTING)) != null) {
			RFIDLightSetting recvdRFIDLightSetting = (RFIDLightSetting) t_dataItem
					.core();
			this.handleRFIDLightSettingProduced(recvdRFIDLightSetting);
		} else if ((t_dataItem = m_dataPool.getData(
				IDConstants.T_PROCESSLIGHTINFO,
				IDConstants.D_CONTROLLERLIGHTSETTING)) != null) {
			ControllerLightSetting recvdControllerLightSetting = (ControllerLightSetting) t_dataItem
					.core();
			this
					.handleControllerLightSettingProduced(recvdControllerLightSetting);
		} else if ((t_dataItem = m_dataPool.getData(
				IDConstants.T_PROCESSLIGHTINFO, IDConstants.D_LIGHTINFOREQUEST)) != null) {
			LightInfoRequest recvdLightInfoRequest = (LightInfoRequest) t_dataItem
					.core();
			this.handleLightInfoRequestProduced(recvdLightInfoRequest);
		}
	}

	private void handleLightReadingProduced(LightReading r_LightReading) {
		Action m_Action = new Action();
		m_Action.setID(this.m_myState.getMyId());
		int ID = r_LightReading.getID();
		lightReading = r_LightReading.getLightReading();

		double diff = lightSetting - lightReading;

		if (diff > 0) {
			m_Action.setActionCode(Constants.ON);
		} else {
			m_Action.setActionCode(Constants.OFF);
		}
		m_myState.debugPrint("[ProcessLightInfo]Got a ID " + ID
				+ ", and light reading " + lightReading);

		m_myState.debugPrint("[ProcessLightInfo] Light Setting Is "+lightSetting);
		DataItem m_dataitem = new DataItem(IDConstants.D_ACTION,
				IDConstants.T_PROCESSLIGHTINFO, m_Action);
		m_dataPool.putData(m_dataitem);
	}

	private void handleRFIDLightSettingProduced(
			RFIDLightSetting r_RFIDLightSetting) {
		int ID = r_RFIDLightSetting.getID();
		lightSetting = r_RFIDLightSetting.getLightSetting();
		m_myState
				.debugPrint("[ProcessLightInfo]Got a ID " + ID
						+ " and Light Setting: "
						+ r_RFIDLightSetting.getLightSetting());
	}

	private void handleControllerLightSettingProduced(
			ControllerLightSetting r_ControllerLightSetting) {
		int ID = r_ControllerLightSetting.getID();
		lightSetting = r_ControllerLightSetting.getLightSetting();
		m_myState.debugPrint("[ProcessLightInfo]Got a ID " + ID
				+ " and Light Setting: "
				+ r_ControllerLightSetting.getLightSetting());
	}

	private void handleLightInfoRequestProduced(
			LightInfoRequest r_LightInfoRequest) {
		int ID = r_LightInfoRequest.getID();
		String partitionLabel = r_LightInfoRequest.getPartitionLabel();
		int partitionNo = r_LightInfoRequest.getPartitionNo();
		m_myState.debugPrint("[ProcessLightInfo]Got a light info request"
                        + " from ID " + ID);
		// Produce the response
		LightInfoResponse m_LightInfoResponse = new LightInfoResponse();
		m_LightInfoResponse.setID(this.m_myState.getMyId());
		m_LightInfoResponse.setLightReading(lightReading);

		DataItem m_dataitem = new DataItem(IDConstants.D_LIGHTINFORESPONSE,
				IDConstants.T_PROCESSLIGHTINFO, m_LightInfoResponse);
		m_dataPool.putData(m_dataitem, partitionLabel, partitionNo);
	}

}
